/*
 * @Author: szx
 * @Date: 2021-10-18 13:10:24
 * @LastEditTime: 2021-10-18 13:39:35
 * @Description:
 * @FilePath: \leetcode\476\476.go
 */
package main

import "math/bits"

// func findComplement(num int) int {
// 	highBit := 0
// 	for i := 1; i < 31; i++ {
// 		if num < 1<<i {
// 			break
// 		}
// 		highBit = i
// 	}
// 	mask := 1<<(highBit+1) - 1
// 	return num ^ mask
// }
func findComplement(num int) int {
	if num == 0 {
		return 1
	}
	return num ^ (1<<bits.Len(uint(num)) - 1)
}

func main() {
	print(findComplement(0))
}
